import {SignData} from './sign'
import Vue from 'vue'
import {isNumber, merge} from 'lodash'

export default {
  methods: {
    validator (parent, prop = [], type) {
      let value = null
      let key = type ? 'checkErrorValue' : 'checkValue'
      parent.$children.map(child => {
        if (child.$el.className === 'el-row') {
          child.$children.map(item => {
            item.$children.map(item2 => {
              if (item2.prop && item2[key]) {
                if (prop.length) {
                  if (prop.indexOf(item2.prop) !== -1) {
                    if (value) {
                      item2[key]()
                    } else {
                      value = item2[key]()
                    }
                  }
                } else {
                  if (value) {
                    item2[key]()
                  } else {
                    value = item2[key]()
                  }
                }
              }
            })
          })
        } else {
          if (child.prop && child[key]) {
            if (prop.length) {
              if (prop.indexOf(child.prop) !== -1) {
                if (value) {
                  child[key]()
                } else {
                  value = child[key]()
                }
              }
            } else {
              if (value) {
                child[key]()
              } else {
                value = child[key]()
              }
            }
          }
        }
      })
      return value
    },
    validatorError (parent) {
      return this.validator(parent, [], true)
    },
    reset (parent) {
      parent.$children.map(child => {
        if (child.reset) {
          child.reset()
        }
      })
    },
    download (url, data) {
      let token = Vue.localStorage.get(Vue.const.COOKIE.KEY_TOKEN) || ''
      let _data = {
        token: token,
        param: data,
        sign: SignData.signFun(data)
      }
      let _url = url + encodeURIComponent(SignData.objToString(_data))
      window.open(_url)
    },
    back (n) {
      let m = -1
      if (isNumber(n)) {
        m = -n
      }
      this.$router.go(m)
    },
    handleSearch () {
    },
    delQueryModel () {
    },
    mergeHeader (header, data) {
      header.map(item => {
        data.map(items => {
          if (item.headerKey === items.headerKey) {
            merge(item, items)
          }
        })
      })
    },
    handleEdit (fn, val) {
      if (this.$store.state.layout.isShowEdit) {
        this.$confirm('信息尚未保存，确定离开吗？', '信息提示', {type: 'warning'}).then(() => {
          fn(val)
        }).catch(() => {
        })
      } else {
        fn(val)
      }
    }
  }
}
